package com.fengye.algorithms.leecode.dp;

/**
 * @Author fengye
 * @Date 2023/5/6 20:09
 **/
public class DeleteOperationForTwoStrings583 {
    public int minDistance(String word1, String word2) {
        int len1 = word1.length();
        int len2 = word2.length();
        int[][] dp = new int[len1+1][len2+1];
        for(int i=1;i<=len1; i++) {
            char c1 = word1.charAt(i-1);
            for(int j=1;j<=len2;j++) {
                char c2 = word2.charAt(j-1);
                if(c1 == c2) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        // 算出最长公共子串
        int maxLen = dp[len1][len2];
        // 每个字符长度减去子串长度，就是结果
        return len1 - maxLen + len2 - maxLen;
    }
}
